package cn.stylefeng.guns.sys.core.listener;

import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyElm;
import cn.stylefeng.guns.sys.modular.system.entity.BtySuixingfu;
import cn.stylefeng.guns.sys.modular.system.service.BtyElmService;
import cn.stylefeng.guns.sys.modular.system.service.BtySuixingfuService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.List;

/*
 * 监听器批处理数据
 * */
public class SuixingfuExcelListener extends AnalysisEventListener<BtySuixingfu> {

    private BtySuixingfuService suixingfuService;

    public SuixingfuExcelListener(){

    }

    public SuixingfuExcelListener(BtySuixingfuService suixingfuService){
        this.suixingfuService = suixingfuService;
    }


    /**
     * 批处理阈值100
     */
    private static final int BATCH_COUNT = 100;
    List<BtySuixingfu> list = new ArrayList<>();

    @Override
    public void invoke(BtySuixingfu basic, AnalysisContext analysisContext) {
//        if(ToolUtil.isNotEmpty(basic.getZdrq())&&basic.getZdrq().length()>10){
//            basic.setZdrq(basic.getZdrq().substring(0,10));
//        }
        if(ToolUtil.isNotEmpty(basic.getDm())){
            list.add(basic);
        }
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        analysisContext.readWorkbookHolder().setDefaultReturnMap(false);
        saveData();
        // log.info("所有数据解析完成！");

    }
    private void saveData(){
        //调用saveData()方法
        if(list.size()>0){
            suixingfuService.insertSuixingfu(list);
        }

    }
}